package com.xlh.outside;


import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;


import com.wzm.param.ApiResultPo.Base;
import com.wzm.param.ApiResultPo.IsEnGloAppReslult;
import com.wzm.param.ApiResultPo.ServerListsReslult;
import com.xlh.Utils.FUti;
import com.xlh.Utils.GsonManager;
import com.xlh.adapter.GameServerListAdapter;
import com.xlh.bean.DataBase.ServerBean;
import com.xlh.bean.GameServerBean;
import com.xlh.conf.CommonConf;
import com.xlh.interf.IUiUpdateHandler;
import com.xlh.lib.Core.FIntent;
import com.xlh.thread.TestTask;
import com.xlh.view.MyGridView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ServerListActivity extends BaseActivity implements IUiUpdateHandler{



	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.game_server_list);
		app = (WzApplication)getApplication();
		//Log.e("TestAPP====",app.getActiveUser());
		ininControlEvent();
	}


	private void ininControlEvent() {


		//设置菜单
		cTxtMenu = (TextView)findViewById(R.id.txt_tag);
		//设置菜单点击事件
		cTxtMenu.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				FIntent fIntent = new FIntent(ServerListActivity.this,SettingActivity.class);
				startActivity(fIntent);
				finish();
			}
		});




		updateHandler = new Handler() {

			@Override
			public void handleMessage(Message msg) {

				final String msgData = msg.getData().getCharSequence("result").toString();
				String type = msg.getData().getString("type");

				progressDlg.dismiss();
				Message m = new Message();
				Bundle b = new Bundle();



				if( type.equals("getSvrListAtServer") &&  msg.getData().get("err").equals("true")){
					normalDialog = new AlertDialog.Builder(ServerListActivity.this);
					normalDialog.setTitle("提示")
							.setMessage("网络异常请退出重新操作!")
							.setPositiveButton("确定", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int which) {

								}
							}).show();


				}else if(type.equals("getSvrListAtServer") ){
					String[] par;
					String res = msg.getData().getCharSequence("result").toString();

					ServerListsReslult data = GsonManager.getIns().jsonToAnyObject(res, ServerListsReslult.class);
					//从本地JSON种获取数据



					if (data.getCode() == 1) {
						//Toast.makeText(ServerListActivity.this, "注册成功!", Toast.LENGTH_SHORT).show();
						for(ServerBean e : data.getData() ){
							GameServerBean d = new GameServerBean();
							par = e.getUrl().split(":");
							if( par.length < 2 ) continue;
							d.setHost( par[0] );
							d.setPort( par[1] );
							d.setWzkey( e.getWzkey() );
							d.setName( e.getSname() );
							gameSvrList.add(d);
						}

						adapter = new GameServerListAdapter(ServerListActivity.this,gameSvrList);

						gView	=	(MyGridView)findViewById(R.id.lv_gamesvr);
						gView.setAdapter(adapter);
					} else {
						Toast.makeText(ServerListActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
					}


				}









				super.handleMessage(msg);
			}
		};


		getSvrHandler =  new Handler() {









				//super.handleMessage(msg);

		};

		progressDlg = new ProgressDialog( ServerListActivity.this);
		//progressDlg.setTitle("进度对话框测试");  k
		progressDlg.setMessage("请稍后,正在获取服务器数据....");
		progressDlg.setIcon(R.drawable.ic_launcher);
		progressDlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progressDlg.setCancelable(false);
		progressDlg.show();
		//首先判断是否开启公版
//		new Thread(new TestTask()
//				.setContext(ServerListActivity.this)
//				.setUrl(CommonConf.API_GET_ENBEL_GLOBAL_APP)
//				.setType("getIsEnGloApp")
//
//
//		).start();


		Thread th = new Thread(new TestTask()
				.setContext(ServerListActivity.this)
				.setUrl(CommonConf.API_SVR_LIST+"username="+app.getActiveUser())
				.setType("getSvrListAtServer")
		);

		th.start();


	}
	


	@Override
	public Handler getHandler() {
		// TODO Auto-generated method stub
		return updateHandler;
	}

	public void runGame(int i){
		GameServerBean e = gameSvrList.get(i);

		app.setActiveServer(e.getHost());
		app.setActivePort(e.getPort());
		app.setWzkey(e.getWzkey());
		FIntent fIntent = new FIntent(ServerListActivity.this, MainActivity.class);
		startActivity(fIntent);
		finish();

	}



	public String userAccount;
	public String userPassword;
	public Handler updateHandler,getSvrHandler;

	public SQLiteDatabase databse;
	private TextView cTxtMenu;
	private   ProgressDialog progressDlg;
	String svrList;
	List<GameServerBean> gameSvrList = new ArrayList<GameServerBean>();
	GameServerListAdapter adapter;
	MyGridView gView;
	WzApplication app;
	private   AlertDialog.Builder normalDialog;

}
